package com.palm.platform.service;

import com.palm.core.anno.impl.AuthHandler;
import com.palm.core.data.MenuItem;
import com.palm.core.util.StringUtil;
import com.palm.platform.Current;
import com.palm.core.MenuManager;
import com.palm.platform.domain.UserInfo;
import org.noear.solon.annotation.Init;
import org.noear.solon.annotation.Inject;
import org.noear.solon.extend.aspect.annotation.Service;
import org.noear.solon.extend.sqltoy.annotation.Db;
import org.sagacity.sqltoy.dao.SqlToyLazyDao;

import java.util.HashSet;
import java.util.List;
import java.util.Objects;
import java.util.Set;

@Service
public class UserService {
    @Inject
    RoleService roleService;
    @Db
    SqlToyLazyDao dao;

    @Init
    public void init(){
        AuthHandler.setUserLoader((userType,userName)->{
            if(Current.MEMBER.equals(userType)){
                return loadMember(userName,null);
            }else{
                return loadUserInfo(userName,null);
            }
        });
    }
    public UserInfo loadMember(String username,String password){
        UserInfo userInfo=new UserInfo();
        userInfo.setUsername(username);
        userInfo.setPassword(password);
        userInfo=dao.loadBySql("member_login_info",userInfo);
        if(userInfo!=null){
            userInfo.setUserType(Current.MEMBER);
            List<MenuItem> menu=(List<MenuItem>) MenuManager.getAllMemberMenu();
            userInfo.setMenu(menu);
            userInfo.setSuper(true);
        }
        return userInfo;
    }
    public UserInfo loadUserInfo(String username,String password){
       // UserInfo info= mapper.findUserInfo(username,password);
       // @Sql("select * from t_user where username=:username #[and password=md5(concat(username,'$',:password))]")
       UserInfo query=new UserInfo();
       query.setUsername(username);
      // query.setPassword(password);
       UserInfo info=dao.loadBySql("select * from t_user where username=:username and enable=1 #[and password=md5(concat(username,'$',:password))]",query);
        if(info!=null){
            if(StringUtil.isNotEmpty(password)){
                if(!Objects.equals(info.getPassword(), StringUtil.md5Hex(username+"$"+password))){
                    return null;
                }
            }
            if("admin".equals(username)){
                //超级管理员权限，有全部菜单
                info.setMenu(MenuManager.getAllMenu(new HashSet<>(),true));
                info.setUserType(Current.USER);
                info.setSuper(true);
                info.setPermissions(MenuManager.allStrPermission());
                info.setRoles(roleService.getAllRole());
            }else{
               Set<String> permissions= roleService.getPermissionOfUser(username);
               info.setPermissions(permissions);
               info.setSuper(false);
               info.setUserType(Current.USER);
               info.setMenu(MenuManager.getAllMenu(permissions,false));
               info.setRoles(roleService.getRoleOfUser(info.getUsername()));
            }

        }
        return info;
    }
}
